package cn.wgoodq.controller;

import cn.wgoodq.domain.Result;
import cn.wgoodq.domain.propDemo.MyProps;
import cn.wgoodq.domain.propDemo.RandomObject;
import cn.wgoodq.domain.propDemo.ReadPropDemo;
import cn.wgoodq.utils.ResultUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * Created by OuKyou on 2017/4/11.
 */
@RestController
@RequestMapping("/prop")
public class PropController {
    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private MyProps myProps;
    @Autowired
    private ReadPropDemo readPropDemo;
    @Autowired
    private RandomObject randomObject;

    @Value("${myProps.simpleProp}")
    private String simpleProp;

    @Value("${strValue}")
    private String strValue;

    @GetMapping("/readProps")
    Result readProps() {
        // 通过注解的方式读取配置文件的信息。
        logger.info("strValue: {}", strValue);
        // 从配置文件中读取配置信息
        logger.info("myProps: {}", myProps.toString());
        // 从配置文件中读取随机属性
        logger.info("randomObject: {}", randomObject.toString());
        // 从配置文件中读取对象
        logger.info("readPropDemo: {}", readPropDemo.toString());

        return ResultUtil.success(myProps.getSimpleProp());
    }

    @GetMapping("/readPropOfObject")
    Result readPropOfObject() {
        // 同一种方式获取的随机值是相同的
        logger.info("${myProps.simpleProp}: {}", simpleProp);
        // 相同的方式反复获取随机值不变
        logger.info("${myProps.simpleProp}: {}", simpleProp);
        // 不同方式获取的随机值是不相同的
        logger.info("myProps.getSimpleProp(): {}", myProps.getSimpleProp());
        // 相同的方式反复获取随机值不变
        logger.info("myProps.getSimpleProp(): {}", myProps.getSimpleProp());
        return ResultUtil.success(myProps.getSimpleProp());
    }
}